/* To calculate your days from birthday to current time */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

static int is_LeapOrFract(const int year) /*Leap year of Fractional year */
{
  if((year%4==0&&year%100!=0)||year%400==0)
	return 366;
  else
	return 365;
}

static int is_Month_day(const int month,const int year) /* How many days of this month */
{
  if(month<1||month>12)
  {
	printf("Wrong number!\n");
	exit(-1);
  }
  if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
	return 31;
  else if(month==2)
  {
	if(is_LeapOrFract(year)==366)
		return 29;
	else
		return 28;
  }
  else
	return 30;
}

static int first_part(const int year,const int month,const int day) /* The last days of birthyear */
{
  int i,sum=0;
  for(i=12;i>month;i--)
  {
	sum+=is_Month_day(i,year);
  }
  return is_Month_day(month,year)-day+sum;
}

static int second_part(const int year,const int nyear) /* The days of the years between birthyear and this year */
{
  int i,sum=0;
  i=year+1;
  for(;i<nyear;i++)
  {
	sum+=is_LeapOrFract(i);
  }
  return sum;
}

static int third_part(const int year,const int month,const int day) /*The past days of this year */
{
  int i,sum=0;
  for(i=1;i<month;i++)
  {
	sum+=is_Month_day(i,year);
  }
  return sum+day;
}

extern int sumBirth(const int year,const int month,const int day)
{
  time_t now;
  struct tm *curtime;
  int nyear,nmonth,nday;
  int sum=0,sumfirst=0,sumsecond=0,sumthird=0;

  time(&now);/* get the arguments of current time (year month and day) */
  curtime=localtime(&now);
  nyear=curtime->tm_year+1900;
  nmonth=curtime->tm_mon+1;
  nday=curtime->tm_mday;

  sumfirst=first_part(year,month,day);

  sumsecond=second_part(year,nyear);

  sumthird=third_part(nyear,nmonth,nday);
  if(year==nyear)
	sum=sumthird-third_part(year,month,day);
  else
	sum=sumfirst+sumsecond+sumthird;
  
  return sum;
}
